home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 1 / LIGHT-ROM 1 (Amiga Library Services)(1994).iso / ffdisks / d886.lha / TrackEd / Sources / SupRout.c < prev    next >
C/C++ Source or Header  |  1993-07-16  |  7KB  |  328 lines

  1.  
  2. /* SupRout.c  Support routines for main loop */
  3.  
  4. #include <exec/memory.h>
  5. #include <intuition/intuition.h>
  6. #include <libraries/gadtools.h>
  7. #include <clib/intuition_protos.h>
  8. #include <clib/gadtools_protos.h>
  9.  
  10. extern struct Window *Window;
  11. extern struct RastPort *RPort;
  12. extern UBYTE *BB;
  13. extern UBYTE CursorX, CursorY, OldCursorX, OldCursorY, RealCursorY;
  14. extern int drive, lastdrive;
  15. extern ULONG CurBlock;
  16.  
  17. UBYTE TDErrText[] = "Trackdisk error    ";
  18.  
  19. UBYTE *TD_Error_Txt[] =
  20. {
  21.    "Not specified",
  22.    "No sector header",
  23.    "Bad sector preamble",
  24.    "Bad sector",
  25.    "Bad header checksum",
  26.    "Bad sector checksum",
  27.    "Too few sectors",
  28.    "Bad sector header",
  29.    "Write protected",
  30.    "Disk changed",
  31.    "Seek error",
  32.    "Not enough memory",
  33.    "Bad unit number",
  34.    "Bad drive type",
  35.    "Drive in use",
  36.    "Post reset"
  37. };
  38.  
  39. extern struct IntuiText Cursor, TestText, TestText2;
  40. #define FourDigs  5
  41. #define TwoDigs   3
  42. extern UBYTE  TrackBuffer[], SectorBuffer[], BlockBuffer[];
  43. extern struct Border FourBorder, FourBorderInv, BoxBorder, TitleBorder,
  44.                      DrivesBorder;
  45. extern struct PropInfo  Schuif_info;
  46. extern struct StringInfo Track_info, Sector_info, Block_info;
  47.  
  48. #define g_DF0    1
  49. #define g_DF1    2
  50. #define g_DF2    3
  51. #define g_DF3    4
  52. #define g_Track  5
  53. #define g_Sector 6
  54. #define g_Block  7
  55. #define g_Schuif 8
  56. #define g_TrUp   9
  57. #define g_TrDown 10
  58. #define g_SeUp   11
  59. #define g_SeDown 12
  60. #define g_BlUp   13
  61. #define g_BlDown 14
  62.  
  63. extern struct Gadget SchuifGadget, BlDownGadget, BlUpGadget, SeDownGadget,
  64.                      SeUpGadget, TrDownGadget, TrUpGadget, BlockGadget,
  65.                      SectorGadget, TrackGadget, DF3Gadget, DF2Gadget,
  66.                      DF1Gadget, DF0Gadget;
  67.  
  68. extern struct Gadget *DriveGadgets[];
  69.  
  70. extern struct NewWindow NewWindow;
  71.  
  72. struct Requester *BlockInput(window)
  73. struct Window *window;
  74. {
  75.    struct Requester *req;
  76.  
  77.    req = (struct Requester *) AllocMem(sizeof(struct Requester), MEMF_PUBLIC);
  78.    if (req)
  79.    {
  80.       InitRequester(req);
  81.       Request(req, window);
  82.       return(req);
  83.    }
  84.    else return(NULL);
  85. }
  86.  
  87. void UnBlockInput(req, window)
  88. struct Requester *req;
  89. struct Window *window;
  90. {
  91.    if (req)
  92.    {
  93.       EndRequest(req, window);
  94.       FreeMem(req, sizeof(struct Requester));
  95.    }
  96. }
  97.  
  98. void ProcessTDError(error)
  99. int error;
  100. {
  101.    UBYTE *ErrExpl;
  102.  
  103.    if (error != 0)
  104.    {
  105.       TDErrText[18] = TDErrText[19] = ' ';
  106.       sprintf(&TDErrText[16], "%d", error);
  107.       if ((error >= 20) && (error <= 35)) ErrExpl = TD_Error_Txt[error - 20];
  108.       else ErrExpl = "Unknown error";
  109.       ShowText2(Window, TDErrText, ErrExpl, NULL);
  110.    }
  111. }
  112.  
  113. UBYTE CXPos(x)
  114. UBYTE x;
  115. {
  116.    UBYTE RX;
  117.  
  118.    if (x < 32) RX = 5 + 3*(x/2) + (x % 2);
  119.    else RX = 55 + (x - 32);
  120.    return(RX);
  121. }
  122.  
  123. void UpDateCursor()
  124. {
  125.    if ((CursorX != OldCursorX) || (CursorY != OldCursorY))
  126.    {
  127.       if (OldCursorX < 128)
  128.          PrintIText(RPort, &Cursor, CXPos(OldCursorX)*8+23L, OldCursorY*8+18L);
  129.       PrintIText(RPort, &Cursor, CXPos(CursorX)*8+23L, CursorY*8+18L);
  130.       OldCursorX = CursorX;
  131.       OldCursorY = CursorY;
  132.    }
  133. }
  134.  
  135. void ByteToHex(byte, string)
  136. UBYTE byte;
  137. UBYTE *string;
  138. {
  139.    UBYTE a;
  140.  
  141.    a = (byte & 0xf0) >> 4;
  142.    if (a >= 10) a += 'A'-10;
  143.    else a += '0';
  144.    string[0] = a;
  145.    a = byte & 0x0f;
  146.    if (a >= 10) a += 'A'-10;
  147.    else a += '0';
  148.    string[1] = a;
  149. }
  150.  
  151. void ShowLine(Memory, line, os)
  152. UBYTE *Memory;
  153. int line;
  154. UWORD os;
  155. {
  156.    int i;
  157.    UBYTE ch;
  158.  
  159.    TestText.IText[0] = ((os+16*line) >> 8) + '0';
  160.    if (TestText.IText[0] > '9') TestText.IText[0] += 'A'-'0';
  161.    ByteToHex((UBYTE) os+16*line, &TestText.IText[1]);
  162.    for (i = 0; i < 16; i++)
  163.    {
  164.       ch = Memory[i+16*line];
  165.       ByteToHex(ch, &TestText.IText[5+3*i]);
  166.       if ((ch & 0x7f) < 32) ch = '.';
  167.       TestText2.IText[i] = ch;
  168.    }
  169.    PrintIText(RPort, &TestText, 23L, 8*line+18L);
  170. }
  171.  
  172. void ShowBuffer(buffer, offset)
  173. UBYTE *buffer;
  174. UWORD offset;
  175. {
  176.    int j;
  177.  
  178.    for (j = 0; j < 16; j++) ShowLine(buffer, j, offset);
  179. }
  180.  
  181. void UpDateSB()
  182. {
  183.    ULONG L;
  184.    int i;
  185.  
  186.    L = (ULONG) Schuif_info.VertPot*16 / 65535;
  187.    i = (int) L;
  188.    ShowBuffer(BB+16*i, 16*i);
  189.    RealCursorY = CursorY + (UWORD) L;
  190.    OldCursorX = 255;
  191.    UpDateCursor();
  192. }
  193.  
  194. void UpDateBlockG()
  195. {
  196.    Block_info.LongInt = Track_info.LongInt*11+Sector_info.LongInt;
  197.    sprintf(BlockBuffer, "%d\x00", Block_info.LongInt);
  198.    RefreshGList(&BlockGadget, Window, NULL, 1);
  199. }
  200.  
  201. void UpDateTrSeG()
  202. {
  203.    Track_info.LongInt = Block_info.LongInt / 11;
  204.    Sector_info.LongInt = Block_info.LongInt % 11;
  205.    sprintf(TrackBuffer, "%d\x00", Track_info.LongInt);
  206.    sprintf(SectorBuffer, "%d\x00", Sector_info.LongInt);
  207.    RefreshGList(&TrackGadget, Window, NULL, 1);
  208.    RefreshGList(&SectorGadget, Window, NULL, 1);
  209. }
  210.  
  211. void CheckBlock()
  212. {
  213.    int td_err;
  214.  
  215.    if ((CurBlock != Block_info.LongInt) || (drive != lastdrive))
  216.    {
  217.       CurBlock = Block_info.LongInt;
  218.       lastdrive = drive;
  219.       td_err = ReadBlocks(drive, (WORD) CurBlock, 1, BB);
  220.       if (td_err) ProcessTDError(td_err);
  221.       UpDateSB();
  222.    }
  223. }
  224.  
  225.  
  226. void Cursor_Neer()
  227. {
  228.    ULONG L;
  229.  
  230.    if (RealCursorY < 31)
  231.    {
  232.       RealCursorY++;
  233.       if (CursorY < 15)
  234.       {
  235.          CursorY++;
  236.          UpDateCursor();
  237.       }
  238.       else
  239.       {
  240.          L = (ULONG) (RealCursorY - 14) * 65535 / 17;
  241.          NewModifyProp(&SchuifGadget, Window, NULL,
  242.             Schuif_info.Flags, 0, (UWORD) L, 0,
  243.             Schuif_info.VertBody, 1);
  244.          UpDateSB();
  245.       }
  246.    }
  247. }
  248.  
  249. void Scroll_Rechts()
  250. {
  251.    if ((CursorX < 47) && (CursorX != 31)) CursorX++;
  252.    else if (CursorX == 31)
  253.    {
  254.       if (RealCursorY < 31)
  255.       {
  256.          CursorX = 0;
  257.          Cursor_Neer();
  258.       }
  259.    }
  260.    else if (CursorX == 47)
  261.    {
  262.       if (RealCursorY < 31)
  263.       {
  264.          CursorX = 32;
  265.          Cursor_Neer();
  266.       }
  267.    }
  268.    UpDateCursor();
  269. }
  270.  
  271. BYTE Hexcode(ch)
  272. UBYTE ch;
  273. {
  274.    UBYTE rch;
  275.  
  276.    if ((ch >= '0') && (ch <= '9')) rch = ch - '0';
  277.    else if ((ch >= 'A') && (ch <= 'F')) rch = ch - 'A' + 10;
  278.    else if ((ch >= 'a') && (ch <= 'f')) rch = ch - 'a' + 10;
  279.    else rch = -1;
  280.    return(rch);
  281. }
  282.  
  283. void Process_VKey(key)
  284. UBYTE key;
  285. {
  286.  
  287.    if ((key >= 32) && (key <= 127))
  288.    {
  289.       if (CursorX >= 32)
  290.       {
  291.          BB[CursorX-32 + 16*RealCursorY] = key;
  292.          ShowLine(BB + (RealCursorY-CursorY)*16, CursorY,
  293.             (RealCursorY-CursorY)*16);
  294.          OldCursorX = 255;
  295.          Scroll_Rechts();
  296.       }
  297.       else
  298.       {
  299.          if (Hexcode(key) >= 0)
  300.          {
  301.             if (CursorX % 2 == 0) BB[CursorX/2 + 16*RealCursorY] =
  302.                BB[CursorX/2 + 16*RealCursorY] & 0x0f | 16*Hexcode(key);
  303.             else BB[CursorX/2 + 16*RealCursorY] =
  304.                BB[CursorX/2 + 16*RealCursorY] & 0xf0 | Hexcode(key);
  305.  
  306.             ShowLine(BB + (RealCursorY-CursorY)*16, CursorY,
  307.                (RealCursorY-CursorY)*16);
  308.             OldCursorX = 255;
  309.             Scroll_Rechts();
  310.          }
  311.       }
  312.    }
  313. }
  314.  
  315. void Process_MPos(MuisX, MuisY)
  316. WORD MuisX, MuisY;
  317. {
  318.    UBYTE CX;
  319.  
  320.    if (MuisX >= 50) CX = MuisX - 50 + 32;
  321.    else if (MuisX <= 47) CX = (MuisX/3)*2 + MuisX % 3;
  322.    else CX = 32;
  323.    CursorX = CX;
  324.    CursorY = MuisY;
  325.    RealCursorY += MuisY - OldCursorY;
  326.    UpDateCursor();
  327. }
  328.